package cn.edu.scau.cmi.zhengweixin.composite.safe;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

public class SafeCompositeController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextArea inputTextArea;

    @FXML
    private TextArea outputTextArea;

    private StringBuilder sb;
    
    @FXML
    void initialize() {
        assert inputTextArea != null : "fx:id=\"inputTextArea\" was not injected: check your FXML file 'SafeComposite.fxml'.";
        assert outputTextArea != null : "fx:id=\"outputTextArea\" was not injected: check your FXML file 'SafeComposite.fxml'.";
        String input = "Composite root = new Composite(\"总经理\");\n" + 
        		"Composite branch1 = new Composite(\"研发经理\");\n" + 
        		"Composite branch2 = new Composite(\"销售经理\");\n" + 
        		"Leaf leaf1 = new Leaf(\"java研发工程师\");\n" + 
        		"Leaf leaf2 = new Leaf(\"c++研发工程师\");\n" + 
        		"root.add(branch1);\n" + 
        		"root.add(branch2);\n" + 
        		"branch1.add(leaf1);\n" + 
        		"branch1.add(leaf2);\n";
        Composite root = new Composite("总经理");
		Composite branch1 = new Composite("研发经理");
		Composite branch2 = new Composite("销售经理");
		Leaf leaf1 = new Leaf("java研发工程师");
		Leaf leaf2 = new Leaf("c++研发工程师");
		root.add(branch1);
		root.add(branch2);
		branch1.add(leaf1);
		branch1.add(leaf2);
		sb = new StringBuilder();
		display(root, 0);
        String output = sb.toString();
        inputTextArea.setText(input);
        outputTextArea.setText(output);
    }
    
    
    private void display(Composite root, int level) {
		sb.append(root.doSomething(level));
		for(Component c: root.getChildren()) {
			if(c instanceof Composite) {
				display((Composite)c, level+1);
			} else {
				sb.append(c.doSomething(level+1));
			}
		}
		
	}

}
